package entity;

import org.junit.Test;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模拟数据库 - 题库
 */
public class EntityContext {

    private Map<Integer, Question> allQuestions=new HashMap<>();
    private Map<Integer, List<Question>> allLevelQuestions=new HashMap<>();
    private ArrayList<User> UserList = new ArrayList<>();
    private Map<Integer, User> UserMap=new HashMap<>();
    private String rule=new String();
    private Map<Integer, QuestionInfo> paper;
    private Map<Integer, List<Integer>> paperAnswer;
    private int score;
    private ExamInformation information=new ExamInformation();

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public Map<Integer, List<Integer>> getPaperAnswer() {
        return paperAnswer;
    }

    public static void main(String[] args) throws IOException {

    }
    public EntityContext() throws IOException {
        UserResolve();
        QuestionResolve();
        QuestionLevelResolve();
        RulesResolve();

    }


    public Map<Integer, QuestionInfo> getPaper() {
        return paper;
    }

    public ArrayList<User> getUserList() {
        return UserList;
    }

    public Map<Integer, Question> getAllQuestions() {
        return allQuestions;
    }

    public Map<Integer, User> getUserMap() {
        return UserMap;
    }

    public String getRule() {
        return rule;
    }
    //	private List/Set/Map
    // rules

    //题库解析
    private  void QuestionLevelResolve() throws IOException {
        String path = EntityContext.class.getClassLoader().getResource("util/corejava.txt").getPath();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
        String s=br.readLine();
        String all=new String();
        /*List<Question> level1=new ArrayList<>();
        List<Question> level2=new ArrayList<>();
        List<Question> level3=new ArrayList<>();
        List<Question> level4=new ArrayList<>();
        List<Question> level5=new ArrayList<>();
        List<Question> level6=new ArrayList<>();
        List<Question> level7=new ArrayList<>();
        List<Question> level8=new ArrayList<>();
        List<Question> level9=new ArrayList<>();
        List<Question> level10=new ArrayList<>();
        allLevelQuestions.put(1,level1);
        allLevelQuestions.put(2,level2);
        allLevelQuestions.put(3,level3);
        allLevelQuestions.put(4,level4);
        allLevelQuestions.put(5,level5);
        allLevelQuestions.put(6,level6);
        allLevelQuestions.put(7,level7);
        allLevelQuestions.put(8,level8);
        allLevelQuestions.put(9,level9);
        allLevelQuestions.put(10,level10);*/

        while (s!=null){
            all+=s+"bj";
            s=br.readLine();
        }
        br.close();
        String[] allQuestion=all.split("@");
        int count=0;
        for (int i=1;i<allQuestion.length;i++){
            Question temp=new Question();
            String[] arr=allQuestion[i].split("bj");
            String[] select={arr[2],arr[3],arr[4],arr[5]};
            temp.setSelect(select);
            temp.setContext(arr[1]);
            //把第一行答案 分数 类型按","分割
            String[]  firstLine=arr[0].split(",");
            String[] right=firstLine[0].split("=");
            String[] score=firstLine[1].split("=");
            String[] level=firstLine[2].split("=");
            //temp.setRight(right[1]);
            temp.setScore(Integer.parseInt(score[1]));
            temp.setLevel(Integer.parseInt(level[1]));
            List<Integer> answer=new ArrayList<>();
            if (right[1].indexOf("/")==-1){
                temp.setType("单选题");
                answer.add(Integer.parseInt(right[1]));
                temp.setRight(answer);
            }
            else {
                String[] answerM=right[1].split("/");
                for (int j = 0; j < answerM.length; j++) {
                    answer.add(Integer.parseInt(answerM[j]));
                    temp.setRight(answer);
                }
                temp.setType("多选题");
            }
            //allQuestions.put(count++,temp);
            //allLevelQuestions.get(temp.getLevel()).add(temp);
            if (allLevelQuestions.get(temp.getLevel())==null){
                allLevelQuestions.put(temp.getLevel(), new ArrayList<Question>());
                allLevelQuestions.get(temp.getLevel()).add(temp);

            }
            else {
                allLevelQuestions.get(temp.getLevel()).add(temp);
            }
        }
    }
    // 题库解析
    private  void QuestionResolve() throws IOException {
        String path = EntityContext.class.getClassLoader().getResource("util/corejava.txt").getPath();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
        String s=br.readLine();
        String all=new String();

        while (s!=null){
            all+=s+"bj";
            s=br.readLine();
        }
        br.close();
        String[] allQuestion=all.split("@");
        int count=0;
        for (int i=1;i<allQuestion.length;i++){
            Question temp=new Question();
            String[] arr=allQuestion[i].split("bj");
            String[] select={arr[2],arr[3],arr[4],arr[5]};
            temp.setSelect(select);
            temp.setContext(arr[1]);
            //把第一行答案 分数 类型按","分割
            String[]  firstLine=arr[0].split(",");
            String[] right=firstLine[0].split("=");
            String[] score=firstLine[1].split("=");
            String[] level=firstLine[2].split("=");
            //temp.setRight(right[1]);
            temp.setScore(Integer.parseInt(score[1]));
            temp.setLevel(Integer.parseInt(level[1]));
            List<Integer> answer=new ArrayList<>();
            if (right[1].indexOf("/")==-1){
                temp.setType("单选题");
                answer.add(Integer.parseInt(right[1]));
                temp.setRight(answer);
            }
            else {
                String[] answerM=right[1].split("/");
                for (int j = 0; j < answerM.length; j++) {
                    answer.add(Integer.parseInt(answerM[j]));
                    temp.setRight(answer);
                }
                temp.setType("多选题");
            }
            allQuestions.put(count++,temp);
        }
    }
    // 用户解析
    private void UserResolve() throws IOException {
        //String path = this.getClass().getResource("../util/user.txt").getPath();
        String path = this.getClass().getClassLoader().getResource("util/user.txt").getPath();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
        String s ;
        while ((s= br.readLine())!=null){
            if (s.startsWith("#")||s.equals("")){
                continue;
            }
            String[] user=s.split(":");
            int id=Integer.parseInt(user[0]);
            String password=user[2];
            String name=user[1];
            long telephone=Long.parseLong(user[3]);
            String email=user[4];
            User user1 = new User(id,password,name,telephone,email);
            //UserList.add(user1);
            UserMap.put(user1.getId(),user1);
            s=br.readLine();
        }
        br.close();
        /*for (User u:UserList) {
            System.out.println(u);
        }*/
    }
    // 考试规则解析
    private void RulesResolve() throws IOException {
        String path = EntityContext.class.getClassLoader().getResource("util/rule.txt").getPath();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
        String s=br.readLine();
        while (s!=null){
            rule+=s+"\n";
            s=br.readLine();
        }
        br.close();
    }

    //有等级的试卷
    public void createLevelPaper() {
        paperAnswer = new HashMap<>();
        paper=new HashMap<>();
        int num=0;
        for (int i = 1; i <= allLevelQuestions.size(); i++) {
            for (int j=1;j<=2;j++){
                num++;
                int n=(int)(Math.random()*(allLevelQuestions.get(i).size()-1))+1;
                Question ques=allLevelQuestions.get(i).get(n);
                QuestionInfo info=new QuestionInfo();
                //题目在试卷里的序号
                info.setNumber(num);
                //建一个比选项多一的数组
                String[] quesArr=new String[ques.getSelect().length+1];
                //将选项和题干放到题目里
                System.arraycopy(ques.getSelect(),0,quesArr,1,ques.getSelect().length);
                quesArr[0]=ques.getContext();
                info.setQuestion(quesArr);
                //将题目加到map里
                paper.put(num,info);
                paperAnswer.put(num,ques.getRight());
            }
        }

    }
    //创建试卷
    public void createPaper() {
        paperAnswer = new HashMap<>();
        paper=new HashMap<>();
       for (int i=1;i<=20;i++){
            int n=(int)(Math.random()*(allQuestions.size()-1))+1;
            Question ques=allQuestions.get(n);
            QuestionInfo info=new QuestionInfo();
            //题目的序号
            info.setNumber(n);
            String[] quesArr=new String[ques.getSelect().length+1];
            //将选项和题干放到题目里
            System.arraycopy(ques.getSelect(),0,quesArr,1,ques.getSelect().length);
            quesArr[0]=ques.getContext();
            info.setQuestion(quesArr);
            //将题目加到map里
            paper.put(i,info);
            paperAnswer.put(i,ques.getRight());
        }
    }

    public void subAnswer(List<Integer> userAnswer) {
        paper.get(information.getQuestionCount()).setUserAnswers(userAnswer);
    }

    public void countDecrease() {
        information.setQuestionCount(information.getQuestionCount()-1);
    }

    public void countAdd() {
        information.setQuestionCount(information.getQuestionCount()+1);
    }

    public Integer getCount() {
        return information.getQuestionCount();
    }
    public void newCount() {
        information.setQuestionCount(1);
    }

    public ExamInformation getInformation() {
        return information;
    }

    public void setUser(User user) {
        information.setUser(user);
    }


    // 其他文件内容的解析
}
